package com.wxxymaker.edu.utils;

import com.blade.kit.CollectionKit;
import com.blade.kit.StringKit;
import com.blade.mvc.WebContext;
import com.blade.mvc.http.Request;
import io.github.biezhi.anima.page.Page;

/**
 * @Author: soft
 * @Date: 2018/7/29 12:01
 * @Desc:
 */
public class HttpUtil {

    public static <T> Page<T> returnPage(Page<T> page) {
        if (CollectionKit.isNotEmpty(page.getRows())) {
            return page;
        }
        return null;
    }

    public static boolean isAjax() {
        Request request = WebContext.request();
        return "XMLHttpRequest".equals(request.header("X-Requested-With")) || request.isAjax();
    }

    /**
     * 验证header中是不是含有规定的key
     * @param key 指定的key
     * @return bool
     */
    public static boolean hasReferer(String key) {
        String referer = WebContext.request().header("Referer");
        return referer.contains(key);
    }

    /**
     * 判断referer是否来自某些uri
     * @param uris uri集合
     * @return 是否
     */
    public static boolean isFrom(String... uris) {
        String referer = WebContext.request().header("Referer");
        if (StringKit.isNotEmpty(referer)) {
            String uri = referer.substring(referer.indexOf("/", 8));
            for (String u : uris) {
                if (u.equals(uri)) return true;
            }
        }
        return false;
    }

    public static String _404() {
        return "error/404";
    }

    public static String _400() {
        return "error/400";
    }

    public static String _500() {
        return "error/500";
    }
}
